package com.czq.controllor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czq.common.Result;
import com.czq.execption.BusinessEXception;
import com.czq.pojo.Employee;
import com.czq.service.EmployeeService;
import com.czq.service.serviceImpl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeControllor {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public Result login(HttpServletRequest request, @RequestBody Employee employee) {
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> query = new LambdaQueryWrapper<>();
        query.eq(Employee::getUsername, username);
        Employee emp = employeeService.getOne(query);
        if (emp == null) {
            return Result.error("登录失败,查询不到用户名");
        }
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!emp.getPassword().equals(password)) {
            return Result.error("密码错误");
        }
        if (emp.getStatus() == 0) {
            return Result.error("账号被禁用");
        }
        request.getSession().setAttribute("employee", emp.getId());
        return Result.success(emp);
    }

    @PostMapping("/logout")
    public Result ligout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");

    }

    @PostMapping()
    public Result<String> addEmployee(HttpServletRequest request, @RequestBody Employee employee) {
        Long empid = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empid);
        employee.setUpdateUser(empid);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        try {
            employeeService.save(employee);
        } catch (Exception e) {
            throw new BusinessEXception("账号重复注册，添加员工失败");
        }

//        employeeService.save(employee);

        return Result.success("添加成功");


    }

    @GetMapping("/page")
    public Result<Page> getAll(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize, String name) {
        Page p = new Page(page, pageSize);

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(p, queryWrapper);

        return Result.success(p);
    }
    @PutMapping
    public Result updateEmployee(HttpServletRequest request,@RequestBody Employee employee){
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
//        Long empid = (Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(empid);
//        employee.setUpdateUser(empid);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);

return Result.success("修改成功");
    }

    //修改的回显操作
    @GetMapping("/{id}")
   public Result<Employee> getbyid(@PathVariable Long id){

        Employee employee = employeeService.getById(id);

        return Result.success(employee);

    }
}
